package com.ruoyi.workflow.service.impl;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.workflow.domain.WfForm;
import com.ruoyi.workflow.domain.WfFormTable;
import com.ruoyi.workflow.domain.dto.WfFormListInput;
import com.ruoyi.workflow.domain.dto.WfFormSaveInput;
import com.ruoyi.workflow.domain.dto.WfFormUpdateInput;
import com.ruoyi.workflow.domain.dto.WfFormView;
import com.ruoyi.workflow.service.IWfFormService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.sql.JSqlClient;
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 流程表单Service业务层处理
 *
 * @author KonBAI
 * @createTime 2022/3/7 22:07
 */

@RequiredArgsConstructor
@Service
public class WfFormServiceImpl implements IWfFormService {

    private final JSqlClient sqlClient;

    // private final WfFormMapper baseMapper;

    /**
     * 查询流程表单
     *
     * @param formId 流程表单ID
     * @return 流程表单
     */
    @Override
    public WfFormView queryById(String formId) {
        return sqlClient.findById(WfFormView.class, formId);
    }

/**
 * 查询流程表单列表
 *
 * @param bo 流程表单
 * @return 流程表单
 */

    /*@Override
    public TableDataInfo<WfFormVo> queryPageList(WfFormBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WfForm> lqw = buildQueryWrapper(bo);
        Page<WfFormVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }*/

    /**
     * 查询流程表单列表
     *
     * @param bo 流程表单
     * @return 流程表单
     */

    @Override
    public List<WfFormView> queryList(WfFormListInput bo) {
        WfFormTable table = WfFormTable.$;
        return sqlClient.createQuery(table)
                .where(table.formName().ilikeIf(bo.getFormName()))
                .select(table.fetch(WfFormView.class))
                .execute();
    }

    /**
     * 新增流程表单
     *
     * @param bo 流程表单
     * @return 结果
     */
    @Override
    public int insertForm(WfFormSaveInput bo) {
        return sqlClient.saveCommand(bo.toEntity())
                .setMode(SaveMode.INSERT_ONLY)
                .execute()
                .getTotalAffectedRowCount();
    }

    /**
     * 修改流程表单
     *
     * @param bo 流程表单
     * @return 结果
     */
    @Override
    public int updateForm(WfFormUpdateInput bo) {
        return sqlClient.saveCommand(bo)
                .setMode(SaveMode.UPDATE_ONLY)
                .execute()
                .getTotalAffectedRowCount();
    }

    /**
     * 批量删除流程表单
     *
     * @param ids 需要删除的流程表单ID
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids) {
        return sqlClient.deleteByIds(WfForm.class, ids)
                .getTotalAffectedRowCount() > 0;
    }

    @Override
    public Page<WfFormView> queryPage(WfFormListInput bo, PageQuery pageQuery) {
        WfFormTable table = WfFormTable.$;
        return sqlClient.createQuery(table)
                .where(table.formName().ilikeIf(bo.getFormName()))
                .select(table.fetch(WfFormView.class))
                .fetchPage(pageQuery.getPageNum(), pageQuery.getPageSize());
    }

    @Override
    public List<WfFormView> listAll(WfFormListInput bo) {
        WfFormTable table = WfFormTable.$;
        return sqlClient.createQuery(table)
                .where(table.formName().ilikeIf(bo.getFormName()))
                .select(table.fetch(WfFormView.class))
                .execute();
    }

}

